#include "apue.h"
#include "error.c"
#include <fcntl.h>

char buf1[] = "abcdefghij";
char buf2[] = "ABCDEFGHIJ";

char buf3[] = "zxcvbnm";

/**
 * 创建一个空洞文件
 * 填部分数据，测试对空洞文件的影响
 */
int main(int argc, char const *argv[])
{
    int fd;
    if((fd = creat("file.hello", FILE_MODE)) < 0)
        err_sys("creat error");
    
    ssize_t wsize;

    if((wsize = write(fd, buf1, sizeof(buf1) - 1)) != 10)
        err_sys("buf1 write error");
    printf("size: %ld\n", wsize);

    off_t old_offset;
    if((old_offset = lseek(fd, 0, SEEK_CUR)) == -1)
        err_sys("old offset log error");

    if(lseek(fd, 16384, SEEK_SET) == -1)
        err_sys("lseek error");
    
    
    if((wsize = write(fd, buf2, sizeof(buf2) - 1)) != 10)
        err_sys("buf2 write error");
    printf("size: %ld\n", wsize);
    
    if(lseek(fd, old_offset, SEEK_SET) == -1)
        err_sys("set old offset error");

    if((wsize = write(fd, buf3, sizeof(buf3) - 1)) != 7)
        err_sys("buf3 write error");
    
    printf("size: %ld\n", wsize);

    if(lseek(fd, 0, SEEK_SET) == -1)
        err_sys("set old offset error");
    
    unsigned long long value = 10000l;

    if((wsize = write(fd, &value, sizeof(value))) == -1)
        err_sys("buf3 write error");
    printf("size: %ld\n", wsize);
    return 0;
}

